package ca.jsbr.canvaswar.games.ui 
{
	import ca.jsbr.as2js_lib.display.animations.Tween;
	import ca.jsbr.as2js_lib.display.canvas.Node2D;
	import ca.jsbr.as2js_lib.display.canvas.view.Image9Grid;
	import ca.jsbr.as2js_lib.display.canvas.view.SpriteView;
	import ca.jsbr.as2js_lib.display.canvas.view.TextView;
	import ca.jsbr.as2js_lib.event.MouseJSEvent;
	import ca.jsbr.as2js_lib.utils.assets.AssetsFactory;
	import ca.jsbr.as2js_lib.utils.MouseManager;
	import ca.jsbr.canvaswar.games.unit.Unit;
	import ca.jsbr.tinyUI.container.VBox;
	import ca.jsbr.tinyUI.input.Button;
	import flash.events.Event;
	/**
	 * ...
	 * @author jsbr [jsricard@lvlstudio.com]
	 */
	public class UiLayer extends Node2D
	{
		public var freeze:Boolean;
		private var _tooltips:Node2D;
		private var _cursor:SpriteView;
		
		public function UiLayer() 
		{
			
		}
		
		public function startMouse():void
		{
			_cursor = new SpriteView(AssetsFactory.getInstance().getAssets("cursor"), 16, 4);
			_cursor.defineAnimation("default", 1, 16);
			_cursor.defineAnimation("attack", 17, 32);
			addChild(_cursor);
			_cursor.play("default");
			MouseManager.getInstance().addEventListener(MouseJSEvent.MOUSE_MOVE, onMouseMove);
		}
		
		
		private function onMouseMove(e:MouseJSEvent):void 
		{
			var cx:int = Math.floor(e.stageX / 32) * 32;
			var cy:int = Math.floor(e.stageY / 32) * 32;
			_cursor.x = cx;
			_cursor.y = cy;
		}
		
		
		private function createPanel(posX:int, posY:int, w:int, h:int):Node2D
		{
			var w = 120;
			var panel:Node2D = new Node2D();
			panel.x = posX;
			panel.y = posY;
			var bg:Image9Grid = new Image9Grid(AssetsFactory.getInstance().getAssets("panel"));
			bg.width = 10;
			bg.height = 10;
			bg.alpha = 0;
			panel.addChild(bg);
			Tween.to(bg, 0.3, { width:w, height:h, alpha:1 } );
			return panel;
		}
		
		public function showToolTips(posX:int, posY:int, infos:Array):void 
		{
			closeToolTips();
			var w = 120;
			
			var txtTitle:TextView = new TextView();
			txtTitle.x = 4;
			txtTitle.y = 4;
			txtTitle.alpha = 0;
			txtTitle.multiline = true;
			txtTitle.textAlign = TextView.LEFT;
			var txtValue:TextView = new TextView();
			txtValue.x = w-8;
			txtValue.alpha = 0;
			txtValue.y = 2;
			txtValue.multiline = true;
			txtValue.textAlign = TextView.RIGHT;
			for (var i:int = 0; i < infos.length; i += 2)
			{
				txtTitle.text += infos[i]+"\n";
				txtValue.text += infos[i+1]+"\n";
			}
			_tooltips = createPanel(posX,posY,w,txtValue.height + 8);
			//bg.height = 100;//txtValue.height+4;
			//Tween.to(bg, 0.3, { width:w, height:txtValue.height + 8, alpha:1 } );
			Tween.to(txtValue, 0.3, { alpha:1,delay:0.3 } );
			Tween.to(txtTitle, 0.2, { alpha:1,delay:0.3 } );
			_tooltips.addChild(txtTitle);
			_tooltips.addChild(txtValue);
			addChild(_tooltips);
		}
		
		public function closeToolTips():void
		{
			if (_tooltips)
			{
				removeChild(_tooltips);
			}
		}
		
		public function showBattleMenu(unit:Unit, unit2:Unit):void 
		{
			closeToolTips();
			var panel:Node2D = new Node2D();
			panel.x=unit2.asset.x;
			panel.y=unit2.asset.y;
			
			var vbox:VBox = new VBox(true);
			var btnAttack:Button = new Button("Attack");
			var btnCancel:Button = new Button("Cancel");
			btnCancel.addEventListener(MouseJSEvent.MOUSE_CLICK,onAttackCancel);
			btnAttack.addEventListener(MouseJSEvent.MOUSE_CLICK,onAttack);
			vbox.addChild(btnAttack);
			vbox.addChild(btnCancel);
			
			_cursor.visible = false;
			
			unit2.asset.x;
			unit2.asset.y;
			vbox.redraw(true);
			panel.addChild(vbox);
			addChild(panel);
			freeze = true;
			_tooltips = panel;
		}
		
		private function onAttack(e:Event):void 
		{
			showBattle(null, null);
		}
		
		private function onAttackCancel(e:Event):void 
		{
			_cursor.visible = true;
			freeze = false;
			closeToolTips();
		}
		
		public function showActionMenu(posX:int, posY:int,options:Array, infos:Array):void 
		{
			
		}
		
		public function showBattle(unit:Unit, unit2:Unit)
		{
			addChild(new Battle(unit, unit2));
		}
	}

}